// pages/vip/regster/regster.js
const app = getApp()
import { api, Countdown } from '../../../utils/config'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone: '',
    pass: '',
    codeValue: '',
    showClear: false,
    showClear1: false,
    btnstatus: true,
    selected: true,
    smsBtnstatus: false,
    smsText: '获取验证码',
    countdown: 60,
    userInfo: {
      mobile: '',
      regMsgCode: '',
      pwd: '',
      source: 13,
      openId: '',
      areaId: '',
      from: 0,
      token: '',
      loginType: 0, //这是要登录微信时动态获取的
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    let openId = 'userInfo.openId'
    let areaId = 'userInfo.areaId'
    let token = 'userInfo.token'
    let loginType = 'userInfo.loginType'
    wx.setNavigationBarTitle({
      title: '绑定手机号',
    })
    // wx.setStorage({
    //   key: "key", //key的后面值是缓存的数值名称
    //   data: { openId: 1, areaId: 9, token: 'abc', loginType:22}  //data的后面值是缓存的数值
    // }) 
    wx.getStorage({
      key:'loginParams', //这里的引号key是缓存里的数值名称
      success:function(res){
        //console.log(res.data.openId, res.data.areaId, res.data.token, res.data.loginType) //这里的引号res.data是缓存里的数值
        that.setData({ [openId]: res.data.openId||'', [areaId]: res.data.areaId||9, [token]: res.data.token, [loginType]: res.data.loginType})
        
    }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.setData({ phone: '', pass: '', codeValue: '' })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  // 判断输入是否为空
  phonenumber: function (e) {
    // console.log(e)
    let that = this
    let mobile = 'userInfo.mobile'
    that.setData({ [mobile]: e.detail.value })
    that.setData({ showClear: true })
    this.setData({ showClear1: false })
  },
  sendcode: function (e) {
    // console.log(e)
    let that = this
    let regMsgCode = 'userInfo.regMsgCode'
    that.setData({ [regMsgCode]: e.detail.value })
    this.setData({ showClear: false })
    this.setData({ showClear1: false })
  },
  password: function(e) {
    let pwd = 'userInfo.pwd'
    let that = this
    that.setData({ [pwd]: e.detail.value })
    that.setData({ showClear1: true })
    this.setData({ showClear: false })
  },
  hasNumber: function () {
    if (this.data.userInfo.mobile.length == 0) {
      this.setData({ showClear: false })
    }
    if (this.data.userInfo.mobile != '') {
      // this.setData({ showClear: false })
      if (this.data.userInfo.regMsgCode != '' && this.data.userInfo.pwd != '' && !this.data.selected) {
        this.setData({ btnstatus: false })
      }
    } else {
      this.setData({btnstatus: true })
    }
  },

  hasCode: function () {
    if (this.data.userInfo.regMsgCode != '') {
      if (this.data.userInfo.mobile != '' && this.data.userInfo.pwd != ''&&!this.data.selected) {
        this.setData({ btnstatus: false })
      }
    } else {
      this.setData({ btnstatus: true })
    }
  },
  hasPass: function() {
    if (this.data.userInfo.pwd.length == 0) {
      this.setData({ showClear1: false })
    }
    if (this.data.userInfo.pwd != '') {
      // this.setData({ showClear1: false })
      if (this.data.userInfo.mobile != '' && this.data.userInfo.regMsgCode != '' && !this.data.selected) {
        this.setData({ btnstatus: false })
      }
    } else {
      this.setData({ btnstatus: true })
    }
  },
  //清除输入
  clearInput: function(e) {
    if(e.currentTarget.dataset.type=='1') {
      this.setData({ showClear: false })
      this.setData({ phone: '' })
    }
    if (e.currentTarget.dataset.type == '2') {
      this.setData({ showClear1: false })
      this.setData({ pass: '' })
    }
  },
  //是否同意协议
  select: function() {
    this.setData({ selected: !this.data.selected })
    if (!this.data.selected && this.data.userInfo.mobile != '' && this.data.userInfo.regMsgCode != '' && this.data.userInfo.pwd != '') {
      this.setData({ btnstatus: false })
    }
  },
  getCode: function (e) {
    let that = this
    // console.log(e)
    if (!/^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$/.test(this.data.userInfo.mobile)) {
      wx.showToast({
        title: '手机号格式不正确',
        icon: 'none',
        duration: 2000,
      })
      return
    }
    let data = {
      mobile: that.data.userInfo.mobile,
      smsType: 1
    }
    
    const url = `${api}/api/sms/yzmMember`
    app.httpClient.post(url, data).then(res => {
      console.log(res)
      if (res.data.code == 200){
        let countdown = that.data.countdown
        Countdown(that, countdown)
        wx.showToast({
          title: "验证码发送成功",
          icon: 'success',
          duration: 2000,
        })
      }
      else{
        wx.showToast({
          title: res.data.msg,
          icon: 'none',
          duration: 2000,
        })
      }
    })
  },
  submit: function () {
    const url = `${api}/api/bind`
    if (!/^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$/.test(this.data.userInfo.mobile)) {
      wx.showToast({
        title: '手机号格式不正确',
        icon: 'none',
        duration: 2000,
      })
      return
    }
    if (this.data.userInfo.regMsgCode =='') {
      wx.showToast({
        title: '验证码不能为空',
        icon: 'none',
        duration: 2000,
      })
      return
    }
    if (this.data.userInfo.pwd == '') {
      wx.showToast({
        title: '密码不能为空',
        icon: 'none',
        duration: 2000,
      })
      return
    }
    if (this.data.userInfo.pwd.length < 6 || this.data.userInfo.pwd.length > 20) {
      wx.showToast({
        title: '密码格式不正确',
        icon: 'none',
        duration: 2000,
      })
      return
    }
    app.httpClient.post(url, this.data.userInfo).then(res => {
      console.log(res)
      if (res.data.code==200) {
        wx.setStorage({
          key: 'isBind',
          data: 'true',
        })
        wx.showToast({
          title: "绑定成功",
          icon: 'success',
          duration: 2000,
        })
        //从跳转过来页面传入路径，然后跳回去
        if (this.redirect != undefined) {
          wx.navigateTo({
            url: this.redirect,
          })
        } 
        else {
          wx.navigateTo({
            url: 'pages/vip/vip',
          })
        }
      }
      else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none',
          duration: 2000,
        })
      }
    })
  }
})